package com.jalarbee.core.bootik.order.business;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import org.hibernate.annotations.Columns;
import org.hibernate.annotations.Type;

import com.jalarbee.core.business.BaseEntity;
import com.jalarbee.core.money.MonetaryAmount;
import com.jalarbee.stock.product.business.Product;
import javax.persistence.JoinColumn;

@Entity
public class OrderItem extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @ManyToOne(optional = false)
    @JoinColumn(name = "ORDER_ID", nullable = false, updatable = false)
    private Order order;
    @ManyToOne
    private Product product;
    private int quantity;
    @Type(type = "com.jalarbee.core.money.MonetaryAmountType")
    @Columns(columns = {
        @Column(name = "P_CURRENCY"),
        @Column(name = "P_AMOUNT")})
    private MonetaryAmount price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public MonetaryAmount getPrice() {
        return price;
    }

    public void setPrice(MonetaryAmount price) {
        this.price = price;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof OrderItem)) {
            return false;
        }
        OrderItem tmp = (OrderItem) obj;
        if (id == tmp.getId()) {
            return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return id * 32 + product.hashCode();
    }

    @Override
    public String toString() {
        return "" + quantity + " x " + product.getName();
    }
}
